#include "Sandbox.hpp"

//////////////////////////////////////////////////////////////////////////
Sandbox::Sandbox()
{
    set_gfx_mode(GFX_AUTODETECT_WINDOWED, 480, 320, 0, 0);
    mScreenBuffer = create_bitmap(480, 320);
    clear(mScreenBuffer);
}

//////////////////////////////////////////////////////////////////////////
Sandbox::~Sandbox()
{
    destroy_bitmap(mScreenBuffer);
}

//////////////////////////////////////////////////////////////////////////
void Sandbox::Add( boost::shared_ptr<Prototype> apPrototype )
{
    mCollection.insert( mCollection.end(), apPrototype);
}

//////////////////////////////////////////////////////////////////////////
void Sandbox::NextPrototype()
{
    if (mCollection.size()>1)
    {
        boost::shared_ptr<Prototype> pActivePrototype = *mActivePrototype;
        pActivePrototype->DeInit();
        PrototypeCollection::iterator iter = mActivePrototype;
        ++iter;
        if (iter==mCollection.end())
        {
            iter = mCollection.begin();
        }
        mActivePrototype = iter;
        pActivePrototype = *mActivePrototype;
        pActivePrototype->Init();
    }
}

//////////////////////////////////////////////////////////////////////////
void Sandbox::PreviousPrototype()
{
    if (mCollection.size()>1)
    {
        boost::shared_ptr<Prototype> pActivePrototype = *mActivePrototype;
        pActivePrototype->DeInit();
        PrototypeCollection::iterator iter = mActivePrototype;
        if (iter==mCollection.begin())
        {
            iter = mCollection.end();
        }
        --iter;
        mActivePrototype = iter;
        pActivePrototype = *mActivePrototype;
        pActivePrototype->Init();
    }
}

//////////////////////////////////////////////////////////////////////////
void Sandbox::Run()
{
    if (key[KEY_PGUP])
    {
        clear_keybuf();
        rest(100);
        PreviousPrototype();
    }
    else if (key[KEY_PGDN])
    {
        clear_keybuf();
        rest(100);
        NextPrototype();
    }
    boost::shared_ptr<Prototype> pActiveProto = *mActivePrototype;
    pActiveProto->Run();
}

//////////////////////////////////////////////////////////////////////////
void Sandbox::Render()
{
    clear(mScreenBuffer);
    boost::shared_ptr<Prototype> pActiveProto = *mActivePrototype;
    pActiveProto->Render(mScreenBuffer);
    char text[64] = { '\0' };
    sprintf(text,"Active prototype: %s", pActiveProto->GetName().c_str());
    textout_ex(mScreenBuffer, font, text, 10, 10, (255,255,255), NULL);
    textout_ex(mScreenBuffer, font, "ESC: Quit", 400, 10, (255,255,255), NULL);
    textout_ex(mScreenBuffer, font, "PageUp: Previous prototype", 10, 300, (255,255,255), NULL);
    textout_ex(mScreenBuffer, font, "PageDown: Next prototype", 280, 300, (255,255,255), NULL);
    acquire_screen();
    blit(mScreenBuffer, screen, 0,0, 0,0, 480, 320);
    release_screen();
}
//////////////////////////////////////////////////////////////////////////
void Sandbox::Init()
{
    ASSERT( mCollection.size>0 );
    mActivePrototype = mCollection.begin();
    boost::shared_ptr<Prototype> pActiveProto = *mActivePrototype;
    pActiveProto->Init();
}
